<template>
  <div>
    <el-page-header icon="" title="企业门户管理系统">
      <template #content>
        <span class="text-large font-600 mr-3"> 个人中心 </span>
      </template>
    </el-page-header>

    <el-row :gutter="20" class="el-row">
      <el-col :span="8">
        <el-card class="box-card1">
          <el-avatar :size="100" :src="avatarUrl" />
          <h3>{{ userStore.userInfo.user_name }}</h3>
          <h5>
            {{ userStore.userInfo.is_admin === true ? "管理员" : "编辑" }}
          </h5>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card class="box-card2">
          <template #header>
            <div class="card-header">
              <span>个人信息</span>
            </div>
          </template>
          <el-form
            ref="userFormRef"
            style="max-width: 600px"
            :model="userForm"
            :rules="userFormRules"
            label-width="auto"
            class="demo-ruleForm"
            status-icon
          >
            <el-form-item label="用户名" prop="user_name">
              <el-input v-model="userForm.user_name" />
            </el-form-item>
            <el-form-item label="性别" prop="gender">
              <el-select
                v-model="userForm.gender"
                placeholder="Select"
                style="width: 100%"
              >
                <el-option label="男" :value="true"></el-option>
                <el-option label="女" :value="false"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="个人简介" prop="introduction">
              <el-input v-model="userForm.introduction" type="textarea" />
            </el-form-item>
            <el-form-item label="头像" prop="avatar">
              <Upload :avatar="userForm.avatar" @uploadChange="handleChange" />
            </el-form-item>
            <el-form-item class="button-container">
              <el-button
                type="primary"
                @click="submitForm()"
                class="custom-button"
              >
                更新
              </el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { computed, ref, reactive } from "vue";
import { useUserStore } from "@/stores/userStore";
import { ElMessage } from "element-plus";
import upload from "@/util/upload";
import Upload from "@/components/upload/Upload.vue";
const userStore = useUserStore();
const avatarUrl = computed(() => {
  // 计算属性，头像改变了就重新赋值一次
  return userStore.userInfo.avatar
    ? "http://localhost:3000" + userStore.userInfo.avatar
    : "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png";
});

const userFormRef = ref();
const { user_name, gender, introduction, avatar } = userStore.userInfo;
const userForm = reactive({
  user_name,
  gender,
  introduction,
  avatar,
  file: null, //用于文件传输
});

const userFormRules = reactive({
  user_name: [{ required: true, message: "请输入名字", trigger: "blur" }],
  gender: [{ required: true, message: "请选择性别", trigger: "blur" }],
  introduction: [
    { required: true, message: "请输入个人介绍", trigger: "blur" },
  ],
  avatar: [{ required: true, message: "请上传头像", trigger: "blur" }],
});

//每次选择完图片之后的回调
const handleChange = (file) => {
  userForm.avatar = URL.createObjectURL(file);
  userForm.file = file; //把表单中放入原生的file对象
};

//更新提交
const submitForm = () => {
  //校验表单
  userFormRef.value.validate(async (valid) => {
    //校验成功的回调
    if (valid) {
      const res = await upload("/adminapi/user/upload", userForm);
      if (res.code === 0) {
        //成功之后要修改state 确保页面中显示的内容和修改后的内容一致
        userStore.changeUserInfo(res.result);
        ElMessage.success("更新成功");
      }
    }
  });
};
</script>

<style scoped lang="scss">
.el-row {
  margin-top: 20px;
  .box-card1 {
    text-align: center;
  }
}
</style>
